﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/29/2009
 * Time: 12:57 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Buaya.Model;
using Buaya.Util;

namespace Buaya.Gui
{
	/// <summary>
	/// Description of LoginForm.
	/// </summary>
	public partial class LoginForm : BaseForm
	{
		public LoginForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			Text = "Login to " + ApplicationUtility.ProductName;
			CompanySingleton.Instance = SessionFacade.ReadCompanyByPk(1);
			labelCompanyName.Text = CompanySingleton.Instance.Name;
		}
		
		void ButtonOkClick(object sender, EventArgs e)
		{
			User u = SessionFacade.ReadUserByNameAndPassword(textBoxName.Text, textBoxPassword.Text);
			if (u == null) {
				MessageService.ShowWarning(
					"The use name or password you typed is incorrect. Please try again or contact your " + Environment.NewLine +
					"administrator for help." + Environment.NewLine + Environment.NewLine + 
					"Passwords are case-sensitive." + Environment.NewLine + Environment.NewLine +
					"For information about troubleshooting or resetting passwords, click Help."
				);
			} else {
				UserSingleton.Instance = u;
				OnUserChanged(null);
				CompanySingleton.Instance = SessionFacade.ReadCompanyByPk(1);
				DialogResult = DialogResult.OK;
				Close();
			}
		}
		
		protected virtual void OnUserChanged(EventArgs e)
		{
			if (UserChanged != null) {
				UserChanged(this, e);
			}
		}
		
		public event EventHandler UserChanged;
	}
}
